package com.zzh.netty.nettyServer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @PackageName:com.zzh.netty.nettyServer
 * @ClassName:NettyClientHandler
 * @Description:
 * @author:zhangzhanheng
 * @date: 2021/1/30 14:32
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    //只要发起连接就会调用这个方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ByteBuf byteBuf = Unpooled.copiedBuffer("HelloServer".getBytes());
        ctx.writeAndFlush(byteBuf);
//        super.channelActive(ctx);
    }
    //读消息
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("收到服务器的消息："+byteBuf.toString(CharsetUtil.UTF_8));
        System.out.println("服务器端的地址："+ctx.channel().remoteAddress());
//        super.channelRead(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        super.exceptionCaught(ctx, cause);
    }
}
